/**
 * Created by ruiyuan on 15-11-17.
 */
'use strict';

var saleApp = angular.module('saleApp', ['ngRoute', 'ngResource', 'marketFilters']);
var params = {};
var tempArray = window.location.search.split('?');
if (tempArray.length == 2) {
    var parts = tempArray[1].split('&');
    for (var i = 0; i < parts.length; i++) {
        var nv = parts[i].split('=');
        if (!nv[0]) continue;
        params[nv[0]] = nv[1] || true;
    }
}
/*var host = 't.app.66xiaoqu.com';*/
/*  宝宝树66小区二次改造    mod   dayu.wedo  20170723   start   */
var host = 'app-66xiaoqu.babytree.com';
//var host = 'app.h2oprint.com';
/*  宝宝树66小区二次改造    mod   dayu.wedo  20170723   end   */
//var host = 'localhost:3000'

saleApp.controller('SaleListCtrl', ['$scope', 'SaleList', 'SaleNotify',
    function ($scope, RecList, SaleNotify) {
        var isMore = true;//判断是否有更多数据
        var isfinish = true;//isfinish判断每次数据是否加载完成，完成了才可进行下一次加载

        $scope.ts = 0;
        $scope.goodsList = [];


        //获取数据
        $scope.getNewData = function (flag) {
            RecList.get({
                ts: $scope.ts
            }, function (result) {
                if ($scope.ts == 0 && result.data.list.length == 0) {
                    $('#sale_end').css('display', 'block');
                    $('.wrapper').css({'overflow': 'hidden'});
                } else {
                    $('#banner').css('display', 'block');
                }

                $scope.ts = result.data.ts;
                if (flag) {
                    $scope.goodsList = result.data.list;
                } else {
                    $scope.goodsList = $scope.goodsList.concat(result.data.list);
                }
                isMore = true;
                isfinish = true;
                if (result.data.list.length == 0) {
                    isMore = false;
                }
                $(".loadingMore").html('');
            });
        };

        $scope.goodsImgW = ($(window).width() - 5) * 0.5 + 'px';
        $scope.goodsImgH = (($(window).width() - 5) * 0.5 / 1.28) + 'px';
        $scope.defaultImg = "images/refresh.gif";


        $('.wrapper').scroll(function () {
            if (isMore && isfinish) {
                var contentHeight = $(".wrapper")[0].scrollHeight;
                var h = $(".wrapper").height();
                if ((contentHeight - h - $(this).scrollTop()) <= 75) {
                    isfinish = false;
                    $(".loadingMore").html('<img src="images/loading.gif" />正在加载');
                    //此处为加载下一条的方法
                    $scope.getNewData(false);
                }
            }
        }).scroll();

        //去二手详情
        $scope.goToDetail = function (objId, createdBy) {
            window.location.href = "detail.html" + window.location.search + '&goodsId=' + objId + '&createdBy=' + createdBy + '&clickSource=702';
        };

        $scope.refresh = function () {
            window.location.reload();
        };

        $scope.goToTopic = function () {
            window.location.href = 'http://66xiaoqu.babytreeimg.com/static/topics/564c2e1d2a3b2cf8370391e0.html';
        };


        if (document.body.clientWidth <= 320) {
            $('.sale_notify').css({'font-size': '0.9em'})

            $('#sale_end').css({'bottom':'-40px'});
        }

        setInterval(function () {
            SaleNotify.get({}, function (result) {
                $scope.notify = result.data;

                if ($scope.notify.time != null && $scope.notify.time > localStorage.getItem('notifyTime')) {
                    $('.sale_notify').css({display: 'none'}).removeClass("sale_notify_animation");
                    setTimeout(function () {
                        $('.sale_notify').addClass('sale_notify_animation').css({display: 'block'});
                    }, 100);
                    localStorage.setItem('notifyTime', $scope.notify.time);
                }
            })
        }, 10000)

    }
]);

saleApp.factory('SaleList', ['$resource',
    function ($resource) {
        return $resource('http://' + host + '/rest/v1.0/market/goods/saleList/:ts/' + params.token + '/' + params.deviceId, {});
    }]);

saleApp.factory('SaleNotify', ['$resource',
    function ($resource) {
        return $resource('http://' + host + '/rest/v1.0/market/saleNotify/' + params.token + '/' + params.deviceId, {});
    }]);
